/**
 * 摄像头截图上传
 */
window.onload = function () {
    layui.use('form',function() {
        var $ = layui.$;
        var canvas = document.getElementById('canvas'),
            context = canvas.getContext('2d'),
            video = document.getElementById("video"),
            img = document.getElementById("canvas_img"),
            snap = document.getElementById("snap"),
            close = document.getElementById("close"),
            start = document.getElementById("start"),
            upload = document.getElementById("upload_button"),
            MediaStreamTrack;
        start.addEventListener('click', function () {
            if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
                navigator.mediaDevices.getUserMedia({
                    video: true,
                    audio: true
                }).then(function (stream) {
                    console.log(stream);
                    MediaStreamTrack=typeof stream.stop==='function'?stream:stream.getTracks()[1];
                    video.src=(window.URL).createObjectURL(stream);
                    video.play();
                }).catch(function(err){
                    console.log(err);
                });
            }else if(navigator.getMedia){
                navigator.getMedia({
                    video: true
                }).then(function (stream) {
                    console.log(stream);
                    MediaStreamTrack=stream.getTracks()[1];
                    video.src=(window.webkitURL).createObjectURL(stream);
                    video.play();
                }).catch(function(err){
                    console.log(err);
                });
            }
        });
        snap.addEventListener('click', function () {
            $('#canvas_img').css('display','none');
            $('#canvas').css('display','block');
            console.log(video);
            context.drawImage(video, 0, 0,200,150);
        });
        close.addEventListener('click', function () {
            MediaStreamTrack && MediaStreamTrack.stop();
        });
        upload.addEventListener('click', function() {
            alert('该功能已取消');
            return false;
            $.post('/admin/upload/savebase64', {
                base64: canvas.toDataURL('image/png')
            }).done(function(data) {
                data = JSON.parse(data);
                layer.msg(data.msg);

                if(data.code == 0) {
                  $('#input_img_identify_url').attr('value' , data.data);
                }
            }).fail(function(err) {
                layer.msg('上传失败');
            });
        }, false);
    });
}
